if(files.cwd().endsWith("脚本")){
  let _require = require;
  require = function(path){
    if(path.startsWith("./")){
      path = path.replace("./","");
    }
    path = files.cwd() + "/gdt_hb/" + path;
    return _require(path);
  }
}


auto.waitFor()
var utils = require("./utils.js");
var storage = storages.create("xiangxiangyuedu");

function launchAppEnhance(app_name) {
  let thread = threads.start(function () {
    toastLog("检查是否有打开权限!")
    while (true) {
      var allow = text("允许").findOne(5000)
      if (allow) {
        sleep(1000)
        toastLog("发现允许授权!")
        allow.click()
        sleep(1000)
        thread.interrupt()
        break
      }
    }
  })
  sleep(1000)
  var result = launchApp(app_name)
  sleep(3000)
  thread.interrupt()
  return result
}

function task(){ 
    var 跳过新手指南 = text("跳过新手指南").findOne(10000);
    if(跳过新手指南){
      log("发现新手指南,并关闭.")
      跳过新手指南.click();
    }
    utils.updateAppProgress(10);
    sleep(1000)
    var _close = id("dialog_dismiss").find(2000);
    if(_close) {
      log("发现首页广告，并关闭.")
      _close.click();
    }

    var 酒链世界 = text("酒链世界").findOne(1000);
    if(!酒链世界){
      toastLog("没有找到 酒链世界")
      return ;
    } 
    酒链世界.click();
    var fab = id("com.js.winechain:id/fab").findOne(1000);
    if(!fab)return ;
    log("进入签到页面.")
    fab.click();
    sleep(6000)
    var thisEle = className("android.view.View").depth(10).findOne(1000);
    if (thisEle) {
      click(356,575);
      log("点击签到.")
      sleep(66000)
    }

    utils.updateAppProgress(50);
    var 酒链世界 = text("酒链世界").findOne(1000);
    if(!酒链世界) return ;
    酒链世界.click();

   // click(412,519,517,624);
   utils.updateAppProgress(70);
    var success = id("com.js.winechain:id/iv_middle_machine").findOne(1000);
    if(!success){
      click(412,519,517,624);
      log("这个干嘛的~ 忘记了;")
    }
    utils.updateAppProgress(90);
    while(!success){
      toastLog("当前是广告页面.");
      sleep(10000);
      success = id("com.js.winechain:id/iv_middle_machine").findOne(1000)
      if(success){
        back();
        log("完成");
      }
    }
  }


function main() {
    let open_app = launchAppEnhance("酒链世界");
    if (open_app) {
      task()
     
    }
    else {
      toastLog("酒链世界打开失败，请确认是否安装并授权打开!");
    }
  }
  
  try {
    main();
    utils.updateAppProgress(100);
  } catch (error) {
    toastLog(error);
  }
  
  storage.put("script_running_status", "end");